package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @author kabuda
 * @create 2022-08-19 10:22
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    OrderSettingService orderSettingService;

    //上传预约文件
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            //读取文件信息存储到list集合中
            List<String[]> list = POIUtils.readExcel(excelFile);
            //新建一个list将string数组信息转换为OrderSetting对象存储
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] strings : list) {
                String date = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting(new Date(date), Integer.parseInt(number));
                orderSettings.add(orderSetting);
            }
            //后台添加信息到数据库
            orderSettingService.add(orderSettings);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    //根据年月份查询对应的预约设置数据
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try{
        List<OrderSetting> list = orderSettingService.getOrderSettingByMonth(date);
        List<Map<String,Object>> resultList=new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                HashMap<String, Object> map = new HashMap<>();
                int day = orderSetting.getOrderDate().getDate();
                int number = orderSetting.getNumber();
                int reservations = orderSetting.getReservations();
                map.put("date",day);
                map.put("number",number);
                map.put("reservations",reservations);
                resultList.add(map);
            }
        }
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,resultList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }


    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try{
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }



}
